/**
 * 所有爬虫的基础类，继承该类，并重写接口方法即可 
 * 这个类对外的接口只有: getPageCount, getItems, buildItem
 * sunny
 * 
 */
import "cheerio";
import "../spider";

const Spider = require("../spider");
const cheerio = require("cheerio");

export class Item {
    title: string; /** 标题, 作为保存数据的查重依据 */
    _create_time: Date; /** 创建时间 */
    source: string; /** 数据来源 */
    _collection: string; /** 数据表 */
};
export class Base {
    version: string;
    collectionShowName:string;
    name: string;
    collection: string;
    enabled: boolean;
    spider: Spider;
    cheerio: CheerioAPI;
    keyField: string;
    _encode: string;
    _debug: boolean;

    constructor() {
        this.version = "1.0.0";
        this.name = "base spider";
        this.enabled = true;
        this.spider = new Spider();
        this._encode = "utf-8";
        this.debug = false;
        this.cheerio = cheerio;
        this.keyField = "title";
    }

    set encode(enc: string) {
        this._encode = enc;
        this.spider.encode = this._encode;
    }

    get encode(): string {
        return this._encode;
    }

    set debug(d: boolean) {
        this._debug = d;
        this.spider.debug = this._debug;
    }

    get debug() {
        return this._debug;
    }

    set raw(r: boolean) {
        this.spider.raw = r;
    }

    get raw(): boolean {
        return this.spider.raw;
    }

    set timeout(t: number) {
        this.spider.timeout = t;
    }

    get timeout(): number {
        return this.spider.timeout;
    }

    /**
     * 获取需要爬取的页面数量
     * 
     */
    getPageCount(): number {
        return 0;
    }

    /**
     * 按照页码获取每页的数据
     * 
     * @param pageIndex 页码
     * 返回待保存的Item集合 
     */
    getItems(pageIndex: number): Item[] {
        return [];
    }

    /**
     * 创建标准的Item
     * 
     */
    buildItem(collection?: string): Item {
        var result: Item = new Item();
        if (collection)
            result._collection = collection;
        else
            result._collection = this.collection;
        result._create_time = new Date();
        result.source = this.name;
        result.title = "";
        return result;
    }
};
